﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import __AS3__.vec.*;

    public class Viewport extends Sprite {

        protected var m_position:Vector2;
        protected var m_zoom:Number = 1;
        protected var m_bitmap:Bitmap;
        protected var m_bitmapData:BitmapData;
        protected var m_transformStack:Array;
        protected var m_absoluteTransformMatrix:Matrix;
        protected var m_layers:Vector.<ViewportLayer>;
        protected var m_bitmapDirty:Boolean = true;

        public function Viewport(){
            this.m_position = new Vector2();
            this.m_bitmap = new Bitmap();
            this.m_transformStack = [];
            this.m_absoluteTransformMatrix = new Matrix();
            this.m_layers = new Vector.<ViewportLayer>();
            super();
            addEventListener(Event.RESIZE, this.onResize);
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            this.initialize();
            InputManager.initializeHandlers(this);
        }
        protected function initialize():void{
            this.addLayer(new ViewportLayer("overlay", -1));
            this.addLayer(new ViewportLayer("object", 0));
            this.addLayer(new ViewportLayer("ui", 1));
            this.addLayer(new ViewportLayer("hud", 2));
        }
        private function onAddedToStage(_arg1:Event):void{
            this.stage.addEventListener(Event.RESIZE, this.onResize);
            this.onResize(null);
        }
        public function getTransformMatrix():Matrix{
            return (this.m_absoluteTransformMatrix);
        }
        public function getScrollPosition():Vector2{
            return (this.m_position.cloneVec2());
        }
        public function setScrollPosition(_arg1:Vector2):void{
            this.m_position = _arg1.cloneVec2();
            this.m_bitmapDirty = true;
        }
        public function setZoom(_arg1:Number):void{
            if (this.m_zoom != _arg1){
                this.m_zoom = _arg1;
                this.m_bitmapDirty = true;
            };
        }
        public function setDirty():void{
            this.m_bitmapDirty = true;
        }
        public function regenerateBitmapData():Boolean{
			
            var _local2:int;
            var _local3:int;
            var _local4:ViewportLayer;
            var _local1:Boolean;
            if (((((!((this.stage == null))) && ((this.stage.stageWidth > 0)))) && ((this.stage.stageHeight > 0)))){
                if ((((((this.m_bitmapData == null)) || (!((this.m_bitmapData.width == this.stage.stageWidth))))) || (!((this.m_bitmapData.height == this.stage.stageHeight))))){
                    if (this.m_bitmap.parent){
                        this.m_bitmap.parent.removeChild(this.m_bitmap);
                    };
                    if (this.m_bitmapData){
                        this.m_bitmapData.dispose();
                        this.m_bitmapData = null;
                    };
                    _local2 = 0;
                    this.m_bitmapData = new BitmapData(this.stage.stageWidth, this.stage.stageHeight);
                    this.m_bitmap.bitmapData = this.m_bitmapData;
                    addChildAt(this.m_bitmap, _local2);
                    _local2++;
                    _local3 = 0;
                    while (_local3 < this.m_layers.length) {
                        _local4 = this.m_layers[_local3];
                        if (_local4.parent){
                            _local4.parent.removeChild(_local4);
                        };
                        addChildAt(_local4, _local2);
                        _local2++;
                        _local3++;
                    };
                };
            };
            return (_local1);
			
        }
        protected function addLayer(_arg1:ViewportLayer):void{
            this.m_layers.push(_arg1);
            this.m_layers.sort(this.sortLayer);
        }
        private function sortLayer(_arg1:ViewportLayer, _arg2:ViewportLayer):Number{
            if (_arg1.priority > _arg2.priority){
                return (1);
            };
            if (_arg1.priority < _arg2.priority){
                return (-1);
            };
            return (0);
        }
        public function getLayer(_arg1:String):Sprite{
            var _local2:Sprite;
            var _local3:int;
            while (_local3 < this.m_layers.length) {
                if (this.m_layers[_local3].name == _arg1){
                    _local2 = (this.m_layers[_local3] as Sprite);
                    break;
                };
                _local3++;
            };
            return (_local2);
        }
        protected function render(_arg1:Boolean=false):Boolean{
            var _local3:Matrix;
            var _local2:Boolean;
            if (((this.m_bitmapData) && (((this.m_bitmapDirty) || (_arg1))))){
                this.regenerateBitmapData();
                this.m_absoluteTransformMatrix.identity();
                this.m_absoluteTransformMatrix.translate(this.m_position.x, this.m_position.y);
                this.m_absoluteTransformMatrix.translate((-(this.stage.stageWidth) / 2), (-(this.stage.stageHeight) / 2));
                this.m_absoluteTransformMatrix.scale(this.m_zoom, this.m_zoom);
                this.m_absoluteTransformMatrix.translate((this.stage.stageWidth / 2), (this.stage.stageHeight / 2));
                this.m_bitmapData.lock();
                this.drawBackground(this.m_bitmapData);
                this.m_bitmapData.unlock();
                _local3 = this.m_absoluteTransformMatrix.clone();
                this.getLayer("object").transform.matrix = _local3;
                this.getLayer("ui").transform.matrix = _local3;
                this.getLayer("overlay").transform.matrix = _local3;
                this.m_bitmapDirty = false;
                _local2 = true;
            };
            return (_local2);
        }
        protected function drawBackground(_arg1:BitmapData):void{
            _arg1.fillRect(_arg1.rect, Config.VIEWPORT_CLEAR_COLOR);
        }
        public function centerViewport():void{
        }
        public function getBitmapData():BitmapData{
            return (this.m_bitmapData);
        }
        public function clearBitmapData():void{
            if (this.m_bitmapData != null){
                this.m_bitmapData.dispose();
                this.m_bitmapData = null;
                this.m_bitmapDirty = true;
            };
        }
        public function getZoom():Number{
            return (this.m_zoom);
        }
        protected function onResize(_arg1:Event):void{
            this.regenerateBitmapData();
            this.m_bitmapDirty = true;
        }
        protected function onEnterFrame(_arg1:Event):void{
            if (this.stage != null){
                this.render();
            };
        }
        public function copyPixels(_arg1:BitmapData, _arg2:Rectangle, _arg3:Point, _arg4:BitmapData, _arg5:Point, _arg6:Boolean):void{
            var _local7:Point = this.m_absoluteTransformMatrix.transformPoint(_arg3);
            this.m_bitmapData.copyPixels(_arg1, _arg2, _local7, _arg4, _arg5, _arg6);
        }
        public function get uiBase():Sprite{
            return (this.getLayer("ui"));
        }
        public function get overlayBase():Sprite{
            return (this.getLayer("overlay"));
        }
        public function get objectBase():Sprite{
            return (this.getLayer("object"));
        }
        public function get hudBase():Sprite{
            return (this.getLayer("hud"));
        }

    }
}//package Engine.Classes 
